<? include_once("../include/session.php"); 
 include_once("../include/dbConnect.php"); ?>
<? 
if($_SERVER['REQUEST_METHOD']=="POST"){ 
	 include("newsProcess.php");
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
	<title>  </title>
<!-- place any meta headers below -->
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
	<meta name="keywords" content="">
	<meta name="description" content="">
<!-- end meta header -->
<!-- place any scripts below -->
	<script src="../include/script/CalendarPopup.js"></script>
	<script src="../include/script/common.js"></script>
	<script src="../include/script/PointerTable.js"></script>
	<script language="JavaScript">document.write(getCalendarStyles());</script>
	<script language="javascript">
<!-- check input form (for required data/ is not null) -->
	 	 function checkForm(){
	 	 if(document.newsForm.news_title.value==""){
	 	 	 document.newsForm.news_title.focus();
	 	 	 alert(" news_title field is still empty, please fill it!");
	 	 	 return false;
	 	 }
	 	 return true;
	 }
	 </script>
<!-- end scripts -->
<!-- place any stylesheets below -->
<link href="../style/common-portal.css" rel="stylesheet" type="text/css">
<!-- end stylesheets -->
</head>
<body onLoad="document.newsForm.elements[3].focus();"> 
<?
// include some function or class 
include_once("../include/session.php");
include_once("../include/dbConnect.php");
//----- end include ------
// get id as primary key 
if($_GET['id']){
	 $id=base64_decode($_GET['id']);
	 $id=str_replace($_SERVER['PHP_SELF'],"",$id);
// query for get a record 
	 $sql=mysql_query("SELECT * FROM news WHERE news_id='$id'");
	 if(mysql_num_rows($sql)){
// fetch record is match 
	 	 $data=mysql_fetch_array($sql);
	 	 $news_id=$data["news_id"];
	 	 $news_title=$data["news_title"];
	 	 $news_content=$data["news_content"];
	 	 $news_post=$data["news_post"];
	 }
}
?>
<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="POST" name="newsForm"   > 
	<table width="780" border="0" class="GeneralTable" celpadding="4" cellspacing="1" >
	<tr><td colspan="2" bgcolor="#FFFFFF" align="right"><? include("../include/headerButton.php");?></td></tr>
	<tr><td class="fieldTable">ID</td><td class="fieldTable"><input type="text" name="news_id" maxlength="12"readonly="true" value="<? echo $news_id; ?>" ></td></tr>
	<tr><td class="fieldTable">Judul</td><td class="fieldTable"><input type="text" name="news_title"  value="<? echo $news_title; ?>" ></td></tr>
	<tr><td class="fieldTable">Isi</td><td class="fieldTable"><textarea cols="50" rows="8" name="news_content"><? echo $news_content; ?></textarea></td></tr>
	<tr><td class="fieldTable">Tgl Post</td><td class="fieldTable"><script language="JavaScript" id=news_post_picker>
	 var news_post_picker= new CalendarPopup();
	 news_post_picker.setReturnFunction("setDate");
	 function setDate(y,m,d) {
	 	 document.forms[0].news_post.value=y+"/"+m+"/"+d;
	 }
</script>
<input type="text" name="news_post" maxlength="19" value="<?  echo $news_post; ?>" > <a href="#" id="news_post_link" title="click here to pick up the date" onClick="news_post_picker.showCalendar('news_post_link'); return false;" ><img src="../images/@Date.gif" border="0"></a></td></tr>
	</table>
<br>
<? 
/* place any include files below */ 
include_once("../include/session.php");
include_once("../include/class/Page.class.php");
include_once("../include/class/Table.class.php");
include_once("../include/class/Search.class.php");
include_once("../include/dbConnect.php");
include_once("../include/function/functHeaderSort.php");
include_once("../include/editButton.php");
/* 
class paging for paging data list/view 
- initPaging = initialize behave of paging, there are four parameter 
  query/sql, number of data will be shown at each page (offset), max page number (don't care about this)
  and query string (piece of url) that represent current page 
*/
$page=new Paging();

 /** 
 Adding sorting function 
*/
$key=array("news_id","news_title","news_content","news_post");
$sort_key=trim($_GET["sort"]); 
$order=$_GET["order"]=="desc"?"desc":"asc"; 
 if($sort_key!="" && count($key)>=(int)$sort_key) 
 { 
	 $sorting=$key[$sort_key]; 
} else { 
	 $sorting=$key[0]; 
} 
 $query="select * from news order by $sorting $order ";

 /** 
 Adding Search Form 
*/
$search=new Search();
$search->initialize("news","news",array("news_id"=>"news_id","news_title"=>"news_title","news_content"=>"news_content","news_post"=>"news_post"));
$query=$search->getQuery($query); 

 /** 
 ------------- End Search Form ----------------
*/
$page->initPaging($query,5,5,"hal_news");
/* 
getQuery = get query/sql after processed by class 
getFirstOffset = get first number of each page 
*/
$query_page=mysql_query($page->getQuery());
$number_page=$page->getFirstOffset();
/*
create class table 
setTableAttributes = set attribute of table as array like width, border, cellpadding, cellspacing 
setCellAttributes = set attribute of cell as array 
setDefaultCellAttributes = set default cell (row-column) attribute 
AddRow = a way to increase row number (adding new row) 
setCellColSpan = set span width of column (colspan) 
setCellContent = insert a cell with some data 
*/
$table=new Table();
/* variable of tables */
$totalColums=4;
$optColums=2;
$width=780;
$optWidth=80;
$table->setTableAttributes(array("class"=>"GeneralTable","width"=>"$width","border"=>"0","cellpadding"=>"0","cellspacing"=>"1"));
$table->setDefaultCellAttributes(array("width"=>round($width/($totalColums+$optColums)),"bgcolor"=>"white"));
/* Add row for title page */
$row=$table->AddRow();
$table->SetCellColSpan( $row, 1,($totalColums+$optColums) );
$table->SetCellAttributes( $row, 1, array("width"=>"100%","class"=>"fieldTable","height"=>"30") );
$table->SetCellContent( $row, 1, "<img src=\"../images/arrow.gif\" border=\"0\"><b> Data list of news</b>" );
/* Add row for info page */ 
$row=$table->AddRow();
$table->SetCellColSpan( $row, 1,($totalColums+$optColums) );
$table->SetCellAttributes( $row, 1, array("align"=>"left","width"=>"100%","class"=>"fieldTable") );
$table->SetCellContent( $row, 1, $page->getInfoPage()."<br>".$page->getPaging());
/* Add row for header field page */ 
$row = $table->AddRow();
$table->SetCellAttribute( $row, 1, "width", "30" );
$table->SetCellContent($row,1,"no");
$table->SetFancyRowStyle( $row, array("bgcolor" => "white", 'style' => 'color: black',"class"=>"labelTable" ) );
/* set cell by header field from database */ 
$table->SetCellAttribute( $row, 2, "width", round((($width-$optWidth)/$totalColums)) );
$table->setCellContent($row,2,"ID ".header_sort(0));
$table->SetCellAttribute( $row, 3, "width", round((($width-$optWidth)/$totalColums)) );
$table->setCellContent($row,3,"Judul ".header_sort(1));
$table->SetCellAttribute( $row, 4, "width", round((($width-$optWidth)/$totalColums)) );
$table->setCellContent($row,4,"Isi ".header_sort(2));
$table->SetCellAttribute( $row, 5, "width", round((($width-$optWidth)/$totalColums)) );
$table->setCellContent($row,5,"Tgl Post ".header_sort(3));
/* one column preserved for edit/delete button */ 
$table->SetCellAttribute( $row, ($totalColums+$optColums), "width", "50" );
$table->setCellContent($row,($totalColums+$optColums) ,"&nbsp;");
$table->SetFancyRowStyle( $row, array("bgcolor" => "white", 'style' => 'color: black',"align"=>"center" ) );
$table->SetFancyRowStyle( $row,array("class"=>"labelTable"));
/* adding row for data  cell */
while($content=mysql_fetch_array($query_page)){
	 $row = $table->AddRow();
	 $table->SetFancyRowStyle( $row,array("class"=>"cellTable"));
/* set cell by record number */
	 $number_page++;
	 $table->SetCellAttribute($row, 1, "width", "30" );
	 $table->SetCellContent( $row, 1, $number_page);
/* set cell by record data */ 
	 $table->SetCellAttribute( $row, 2, "width", round((($width-$optWidth)/$totalColums)) );
	 $table->SetCellContent( $row, 2, ($content["news_id"]!=""?$content["news_id"]:"&nbsp; ") );
	 $table->SetCellAttribute( $row, 3, "width", round((($width-$optWidth)/$totalColums)) );
	 $table->SetCellContent( $row, 3, ($content["news_title"]!=""?$content["news_title"]:"&nbsp; ") );
	 $table->SetCellAttribute( $row, 4, "width", round((($width-$optWidth)/$totalColums)) );
	 $table->SetCellContent( $row, 4, ($content["news_content"]!=""?$content["news_content"]:"&nbsp; ") );
	 $table->SetCellAttribute( $row, 5, "width", round((($width-$optWidth)/$totalColums)) );
	 $table->SetCellContent( $row, 5, ($content["news_post"]!=""?$content["news_post"]:"&nbsp; ") );
/* set cell by control button */ 
	 $table->SetCellAttributes( $row, $totalColums+2, array("width"=>"50","nowrap"=>true) );
	 $table->SetCellContent( $row, $totalColums+2 , editButton($content[0]));
}
/* Add row for info page */ 
$row=$table->AddRow();
$table->SetCellColSpan( $row, 1,($totalColums+$optColums) );
$table->SetCellAttributes( $row, 1, array("align"=>"left","width"=>"100%","class"=>"fieldTable") );
$table->SetCellContent( $row, 1, $page->getInfoPage()."<br>".$page->getPaging());
/*  set double color cross rows */ 
$table->set2RowColors( "white", "#eeeeee", 3, $row );
$table->PrintTable();
?>
</form>
</body></html>